1 module hip.api.data.asset; 2 import hip.api.data.commons; 3 4 abstract class HipAsset 5 { 6 /** Use it to insert into an asset pool, alias*/ 7 protected string _name; 8 /**Currently not in use */ 9 protected uint _assetID; 10 /** Usage inside asset manager */ 11 protected uint _typeID; 12 13 ///How much time it took to load, in millis 14 float loadTime = 0; 15 16 this(string assetName) 17 { 18 this.name = assetName; 19 _assetID = ++currentAssetID; 20 } 21 22 string name() const{return _name;} 23 string name(string newName) {return _name = newName;} 24 uint assetID() const {return _assetID;} 25 uint typeID() const { return _typeID;} 26 27 /** 28 * Action for when the asset finishes loading 29 * Proabably be executed on the main thread 30 */ 31 abstract void onFinishLoading(); 32 abstract bool isReady() const; 33 ///Use it to clear the engine. 34 abstract void onDispose(); 35 36 37 void startLoading() 38 { 39 import hip.util.time; 40 loadTime = HipTime.getCurrentTimeAsMs(); 41 } 42 43 void finishLoading() 44 { 45 import hip.util.time; 46 if(isReady()) 47 { 48 onFinishLoading(); 49 loadTime = HipTime.getCurrentTimeAsMs() - loadTime; 50 } 51 } 52 53 /** 54 * Currently, no AssetID recycle is in mind. It will only invalidate 55 * the asset for disposing it on an appropriate time 56 */ 57 final void dispose() 58 { 59 _assetID = 0; 60 onDispose(); 61 } 62 63 } 64 65 66 class HipFileAsset : HipAsset 67 { 68 string path; 69 ubyte[] data; 70 this(in string path) 71 { 72 super("File_"~path); 73 this.path = path; 74 _typeID = assetTypeID!HipFileAsset; 75 } 76 void load(in ubyte[] data){this.data = cast(ubyte[])data;} 77 string getText(){return cast(string)data;} 78 override void onFinishLoading(){} 79 override void onDispose(){} 80 override bool isReady() const {return data.length != 0;} 81 } 82 83 /** 84 * Controls the asset ids for every game asset 85 * 0 is reserved for errors. 86 */ 87 private __gshared uint currentAssetID = 0; 88 89 private __gshared int assetIds = 0; 90 int assetTypeID(T : HipAsset)() 91 { 92 __gshared int id = -1; 93 if(id == -1){id = ++assetIds;} 94 return id; 95 } 96